<template>
  <div id="app">
    <el-container class="app-container">
      <!-- 头部导航 -->
      <el-header class="app-header">
        <el-menu
          mode="horizontal"
          :default-active="activeIndex"
          @select="handleSelect"
          class="menu-container"
        >
          <el-menu-item index="1">
            <el-icon><House /></el-icon>
            首页
          </el-menu-item>
          <el-menu-item index="2">
            <el-icon><User /></el-icon>
            用户管理
          </el-menu-item>
          <el-menu-item index="3">
            <el-icon><Setting /></el-icon>
            系统设置
          </el-menu-item>
        </el-menu>
      </el-header>

      <!-- 主要内容区域 -->
      <el-main class="app-main">
        <div class="welcome-section">
          <el-card class="welcome-card">
            <template #header>
              <div class="card-header">
                <span>欢迎使用 Vue 3 + Element Plus</span>
                <el-button type="primary" @click="showMessage">
                  <el-icon><Promotion /></el-icon>
                  点击测试
                </el-button>
              </div>
            </template>
            
            <div class="content-section">
              <el-row :gutter="20">
                <el-col :span="12">
                  <el-form :model="form" label-width="80px">
                    <el-form-item label="用户名">
                      <el-input v-model="form.username" placeholder="请输入用户名" />
                    </el-form-item>
                    <el-form-item label="邮箱">
                      <el-input v-model="form.email" placeholder="请输入邮箱" />
                    </el-form-item>
                    <el-form-item label="角色">
                      <el-select v-model="form.role" placeholder="请选择角色">
                        <el-option label="管理员" value="admin" />
                        <el-option label="用户" value="user" />
                        <el-option label="访客" value="guest" />
                      </el-select>
                    </el-form-item>
                    <el-form-item>
                      <el-button type="primary" @click="submitForm">提交</el-button>
                      <el-button @click="resetForm">重置</el-button>
                    </el-form-item>
                  </el-form>
                </el-col>
                
                <el-col :span="12">
                  <el-table :data="tableData" style="width: 100%">
                    <el-table-column prop="date" label="日期" width="180" />
                    <el-table-column prop="name" label="姓名" width="180" />
                    <el-table-column prop="address" label="地址" />
                    <el-table-column label="操作">
                      <template #default="scope">
                        <el-button size="small" @click="handleEdit(scope.$index, scope.row)">
                          编辑
                        </el-button>
                        <el-button size="small" type="danger" @click="handleDelete(scope.$index, scope.row)">
                          删除
                        </el-button>
                      </template>
                    </el-table-column>
                  </el-table>
                </el-col>
              </el-row>
            </div>
          </el-card>
        </div>
      </el-main>

      <!-- 底部 -->
      <el-footer class="app-footer">
        <div class="footer-content">
          <span>Vue 3 + Element Plus 演示项目</span>
        </div>
      </el-footer>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { Action } from 'element-plus'

// 响应式数据
const activeIndex = ref('1')

const form = reactive({
  username: '',
  email: '',
  role: ''
})

const tableData = ref([
  {
    date: '2024-01-01',
    name: '张三',
    address: '北京市朝阳区'
  },
  {
    date: '2024-01-02',
    name: '李四',
    address: '上海市浦东新区'
  },
  {
    date: '2024-01-03',
    name: '王五',
    address: '广州市天河区'
  }
])

// 方法
const handleSelect = (key: string) => {
  activeIndex.value = key
  ElMessage.success(`切换到菜单项: ${key}`)
}

const showMessage = () => {
  ElMessage.success('Element Plus 组件工作正常！')
}

const submitForm = () => {
  if (!form.username || !form.email || !form.role) {
    ElMessage.warning('请填写完整信息')
    return
  }
  
  ElMessage.success(`提交成功！用户名: ${form.username}, 邮箱: ${form.email}, 角色: ${form.role}`)
}

const resetForm = () => {
  form.username = ''
  form.email = ''
  form.role = ''
  ElMessage.info('表单已重置')
}

const handleEdit = (index: number, row: any) => {
  ElMessageBox.confirm(
    `确定要编辑用户 ${row.name} 的信息吗？`,
    '编辑确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {
    ElMessage.success(`开始编辑用户: ${row.name}`)
  }).catch(() => {
    ElMessage.info('取消编辑')
  })
}

const handleDelete = (index: number, row: any) => {
  ElMessageBox.confirm(
    `确定要删除用户 ${row.name} 吗？此操作不可恢复。`,
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'error',
    }
  ).then(() => {
    tableData.value.splice(index, 1)
    ElMessage.success('删除成功')
  }).catch(() => {
    ElMessage.info('取消删除')
  })
}
</script>

<style scoped>
.app-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  background-color: #409eff;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.menu-container {
  background-color: transparent;
  border-bottom: none;
}

.menu-container .el-menu-item {
  color: white;
  font-weight: 500;
}

.menu-container .el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.menu-container .el-menu-item.is-active {
  background-color: rgba(255, 255, 255, 0.2);
}

.app-main {
  flex: 1;
  padding: 20px;
  background-color: #f5f7fa;
}

.welcome-section {
  max-width: 1200px;
  margin: 0 auto;
}

.welcome-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: 600;
}

.content-section {
  margin-top: 20px;
}

.app-footer {
  background-color: #909399;
  color: white;
  text-align: center;
  line-height: 60px;
}

.footer-content {
  font-size: 14px;
}

.el-form {
  padding: 20px;
  background-color: #f8f9fa;
  border-radius: 8px;
}

.el-table {
  margin-top: 20px;
}
</style>